package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.base.SearchPara;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.dto.MetaFieldDTO;
import com.linsir.lin.dto.MetaObjectDTO;
import com.linsir.lin.mapper.MetaFieldMapper;
import com.linsir.lin.pojo.MetaField;
import com.linsir.lin.pojo.MetaObject;
import com.linsir.lin.service.BaseCodeService;
import com.linsir.lin.service.MetaFieldService;
import com.linsir.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName:linsir-lin
 * @Package:com.linsir.lin.service
 * @Title: MetaFieldServiceImpl
 * @Description: TODO
 * @Create:2021/6/13 0:52 by @author:Linsir
 * @Update:2021/6/13 0:52 by @author:Linsir
 * @UpdateRemark: 初始创建
 * @Version 1.0.0
 */
@Service
public class MetaFieldServiceImpl extends ServiceImpl<MetaFieldMapper, MetaField> implements IService<MetaField>, MetaFieldService {


    @Autowired
    private BaseCodeService baseCodeService;

    /**
     * @param queryWrapper
     * @return
     */
    @Override
    public List<MetaField> listByQueryWrapper(QueryWrapper<MetaField> queryWrapper) {
        return list(queryWrapper);
    }

    /**
     * 列表页面查询条件的控件
     *
     * @param
     * @return
     */
    @Override
    public List<MetaFieldDTO> listGridQueryField(String objectCode) throws InvocationTargetException, IllegalAccessException {

        QueryWrapper<MetaField> queryWrapper = new QueryWrapper<MetaField>().
                eq("object_code",objectCode).
                eq("is_query",true);
        return toDtoList(listByQueryWrapper(queryWrapper));
    }


    /**
     * @param objectCode
     * @return
     */
    @Override
    public List<MetaFieldDTO> listByObjectCode(String objectCode) throws InvocationTargetException, IllegalAccessException {
        QueryWrapper<MetaField>  queryWrapper= new  QueryWrapper<MetaField>().eq("object_code",objectCode);
        List<MetaFieldDTO> metaFieldDTOS = toDtoList(listByQueryWrapper(queryWrapper));
        return metaFieldDTOS;
    }

    /**
     * 列表
     *
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public GridDTO<MetaFieldDTO> grid(String page, String limit, String searchParams) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String,String> map = SearchPara.toMap(searchParams);
        QueryWrapper<MetaField> wrapper=new QueryWrapper<MetaField>();
        if (map!=null)
        {
            wrapper.eq("object_code",map.get("objectCode"));
        }

        Page<MetaField> pageDto = new PageDto<MetaField>();
        pageDto.setCurrent(Long.parseLong(page));
        pageDto.setSize(Long.parseLong(limit));
        pageDto = super.baseMapper.selectPage(pageDto,wrapper);
        List<MetaFieldDTO> metaFieldDTOList =toDtoList(pageDto.getRecords());
        return new GridDTO<MetaFieldDTO>("0","成功",pageDto.getTotal(),metaFieldDTOList);
    }

    /**
     * to dto
     *
     * @param metaField
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public MetaFieldDTO toDto(MetaField metaField) throws InvocationTargetException, IllegalAccessException {
        MetaFieldDTO metaFieldDTO = new MetaFieldDTO();
        BeanUtils.copyProperties(metaFieldDTO,metaField);

        String isAutoName = baseCodeService.codeName("isAuto",String.valueOf(metaField.isAuto()));
        String isQueryName = baseCodeService.codeName("isQuery",String.valueOf(metaField.isQuery()));
        String isShowName = baseCodeService.codeName("isShow",String.valueOf(metaField.isShow()));
        String isDisableName = baseCodeService.codeName("isDisable",String.valueOf(metaField.isDisable()));
        String isOrderName = baseCodeService.codeName("isOrder",String.valueOf(metaField.isOrder()));
        String isAddName = baseCodeService.codeName("isAdd",String.valueOf(metaField.isAdd()));
        String isUpdateName = baseCodeService.codeName("isUpdate",String.valueOf(metaField.isUpdate()));
        String isEditName = baseCodeService.codeName("isEdit",String.valueOf(metaField.isEdit()));
        String isRequiredName = baseCodeService.codeName("isRequired",String.valueOf(metaField.isRequired()));
        String isMultipleName = baseCodeService.codeName("isMultiple",String.valueOf(metaField.isMultiple()));
        String addStatusName=baseCodeService.codeName("addStatus",metaField.getAddStatus().toString());
        String updateStatusName=baseCodeService.codeName("updateStatus",metaField.getUpdateStatus().toString());

        String col = StringUtils.fieldCol(metaField);

        metaFieldDTO.setIsAddName(isAutoName);
        metaFieldDTO.setIsQueryName(isQueryName);
        metaFieldDTO.setIsShowName(isShowName);
        metaFieldDTO.setIsDisableName(isDisableName);
        metaFieldDTO.setIsOrderName(isOrderName);
        metaFieldDTO.setIsAddName(isAddName);
        metaFieldDTO.setIsUpdateName(isUpdateName);
        metaFieldDTO.setIsEditName(isEditName);
        metaFieldDTO.setIsRequiredName(isRequiredName);
        metaFieldDTO.setIsMultipleName(isMultipleName);
        metaFieldDTO.setAddStatusName(addStatusName);
        metaFieldDTO.setUpdateStatusName(updateStatusName);
        metaFieldDTO.setCol(col);


        return metaFieldDTO;
    }

    /**
     * to dto list
     *
     * @param metaFieldList
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public List<MetaFieldDTO> toDtoList(List<MetaField> metaFieldList) throws InvocationTargetException, IllegalAccessException {
        List<MetaFieldDTO>  metaFieldDTOS = new ArrayList<MetaFieldDTO>();
        for (MetaField metaField : metaFieldList)
        {
            metaFieldDTOS.add(toDto(metaField));
        }
        return  metaFieldDTOS;
    }


}
